package com.utils;

import cn.hutool.extra.mail.MailUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

/**
 * @author fanshihao
 * @date 2025-03-26 16:50
 */
@Component
@Slf4j
public class MailUtils {

    @Autowired
    private JavaMailSender mailSender;

    /**
     * 发送邮件
     * @param to 收件人邮箱
     * @param subject 邮件主题
     * @param content 邮件内容
     */
    public void sendMail(String to, String subject, String content) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom("2351287447@qq.com"); // 发件人
            message.setTo(to); // 收件人
            message.setSubject(subject); // 邮件主题
            message.setText(content); // 邮件正文
            mailSender.send(message);
            System.out.println("邮件发送成功！");
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }
}
